<?php

/**
 * Class cash 提现管理
 */
Class cashControl extends BaseSellerControl{

    /**
     * 提现管理列表
     */
     public function indexOp(){


         $state = intval($_GET['state']);
         $paystate = intval($_GET['paystate']);
         $model_pd = Model('predeposit');
         $condition = "pdc_payment_state = '$paystate' and state = $state ";
         $keyword = trim($_GET['name']);
         if($keyword){
             if(is_numeric($keyword)){
                 $str = " and pdc_sn = $keyword";
             }else{
                 $str = " and pdc_member_name like '%$keyword%'";
             }
         }
         $cash_list = $model_pd->getPdCashList($condition.$str,5,'*','pdc_add_time desc');
         Tpl::output('list',$cash_list);
         Tpl::output('name', $keyword);
         Tpl::output('show_page',$model_pd->showpage());
         Tpl::showpage('cash_index');




//         $condition = array();
//         if($_GET['state']){
//                $condition['state'] = $_GET['state'];
//         }else{
//                $condition['state'] = 0;
//         }
//         $condition['class'] = 0;
//         $cash = model("predeposit");
//         $cash_list = $cash->getCashList($condition,10);
//         foreach($cash_list as $k => $v){
//             $member = Model('member')->getMemberInfoByID($v['member_id']);
//             $cash_list[$k]['member_name'] = $member['member_name'];
//         }
//         Tpl::output('class',0);
//         Tpl::output('list',$cash_list);
//         Tpl::output('show_page',$cash->showpage());
//         Tpl::showpage('cash_index');
     }


    /*
        * 提现搜索
        *
        */
    public function pdcash_pushOp(){
        $condition = array();
        if (!empty($_GET['sn_search'])) {
            $condition['pdc_sn'] = $_GET['sn_search'];
        }
        if (isset($_GET['paystate_search'])){
            $condition['pdc_payment_state'] = intval($_GET['paystate_search']);
        }
        $model_pd = Model('predeposit');
        $cash_list = $model_pd->getPdCashList($condition,30,'*','pdc_id desc');

        Tpl::assign('list',$cash_list);
        Tpl::assign('show_page',$model_pd->showpage());
        Tpl::display('store_pdcash_index');

    }


    /**
      * 提现编辑
      */
    public function editOp(){

        $where['pdc_id'] = intval($_GET['id']);
        $model_pd = Model('predeposit');
        $info = $model_pd->getPdCashInfo($where);
        $card = Model('member_card')->where(['member_id' => $info['pdc_member_id']])->find();
        Tpl::output('card',$card);
        Tpl::output('info',$info);
        Tpl::showpage('cash_edit');


//        $where['id'] = intval($_GET['id']);
//        $model_pd = Model('cash_withdrawal_apply');
//        $info = $model_pd->where($where)->find();
//        $member = Model('member')->getMemberInfoByID($info['member_id']);
//        $info['member_name'] = $member['member_name'];
//        Tpl::output('info',$info);
//        Tpl::showpage("cash_edit");
    }

    /**
     * 保存编辑
     */
    public function save_editOp(){

        $where['pdc_id'] = intval($_POST['pdc_id']);
        $data['state'] = intval($_POST['status']);
        $data['remark'] = trim($_POST['remark']);
        $data['pdc_payment_time'] = time();
        if($data['state'] == 1){
            $data['pdc_payment_state'] = 1;
            $change_type = 'cash_pay';
        }else{
            $data['pdc_payment_state'] = 0;
            $change_type = 'cash_del';
        }
        $model_pd = Model('predeposit');
        $info = $model_pd->editPdCash($data,$where);
        if($info){
            $pd_data['member_id'] = $_POST['pdc_member_id'];
            $pd_data['member_name'] = $_POST['pdc_member_name'];
            $pd_data['amount'] = $_POST['pdc_amount'];
            $pd_data['order_sn'] = $_POST['pdc_sn'];
            $res = $model_pd->changePd($change_type, $pd_data);
            if($res){
                showDialog('操作成功！',  urlShop('cash','index'),'succ');
            }else{
                showDialog('操作失败！',  urlShop('cash','index'));
            }
        }else{
            showDialog('操作失败！',  urlShop('cash','index'));
        }





//        $where['id'] = $_POST['pdc_id'];
//        $data['state'] = intval($_POST['status']);
//        $data['remarks'] = intval($_POST['remark']);
//        $class = $_POST['class'];
//        $data['verification_time'] = time();
//        $cash = model("predeposit");
//        $info = $cash->editCash($data,$where);
//        if($info) {
//             //冻结款清零
//             $condition['member_id'] = $_POST['pdc_member_id'];
//             $member_info = Model('member')->getMemberInfoByID($_POST['pdc_member_id']);
//             if($_POST['status']==1) {
//                 // 审核成功
//                 if ($class == 0) {
//                     $updata['freeze_predeposit'] = 0;
//                 }
//                 if ($class == 1) {
//                     $updata['store_freeze_brokerage'] = 0;
//                 }
//                 if ($class == 2) {
//                     $updata['freeze_brokerage'] = 0;
//                 }
//             }else{
//                 // 审核失败
//                 if ($class == 0) {
//                     $updata['available_predeposit'] = $member_info['available_predeposit']+$member_info['freeze_predeposit'];
//                     $updata['freeze_predeposit'] = 0;
//                 }
//                 if ($class == 1) {
//                     $updata['av_store_brokerage'] = $member_info['av_store_brokerage']+$member_info['store_freeze_brokerage'];
//                     $updata['store_freeze_brokerage'] = 0;
//                 }
//                 if ($class == 2) {
//                     $updata['av_brokerage'] = $member_info['av_brokerage']+$member_info['freeze_brokerage'];
//                     $updata['freeze_brokerage'] = 0;
//                 }
//             }
//              $res = Model('member')->editMember($condition,$updata);
//              if($res){
//                  if($res){
//                      showDialog('操作成功！',  urlShop('cash','index'),'succ');
//                  }else{
//                      showDialog('操作失败！',  urlShop('cash','index'));
//                  }
//              }
//        }else{
//            showDialog('操作失败！',  urlShop('cash','index'));
//        }
    }

    //导出提现表单
    public function export_pushOp() {
        $info = '';
        if (is_post()) {
            $status = explode('|',$_POST['select_state']);
            $state = $status[0];
            $pdc_payment_state = $status[1];
            $stime = strtotime($_POST['startdate']);
            $etime = strtotime($_POST['enddate']);
            $startnum = intval($_POST['startnum']);
            $endnum = intval($_POST['endnum']);
            $snum = empty($startnum) ? 0 : $startnum;
            $enum = empty($endnum) ? 2000 : $endnum;
            $stime > time() && showMessage('起止时间不能大于今天!', '', '', 'error');
            $etime > time() && showMessage('结束时间不能大于今天！', '', '', 'error');
            $stime > $etime && showMessage('起止时间不能大于结束时间！', '', '', 'error');
            ($enum - $snum > 2000) && showMessage('查询条数不能大于2000条', '', '', 'error');
            ($enum - $snum < 0) && showMessage('结束条数不能小于起始条数', '', '', 'error');
            $where = "store_id = ".$_SESSION['store_id']." AND state = $state AND pdc_payment_state = $pdc_payment_state";
            if ($stime) {
                if ($etime) {
                    if($stime == $etime){
                        $time = date('Y-m-d',$stime);
                        $stime = strtotime($time);
                        $where .= " AND pdc_add_time >= $stime";
                    }else{
                        $where .= " AND pdc_add_time >= $stime AND pdc_add_time < $etime";
                    }
                } else {
                    $where .= " AND pdc_add_time >= $stime ";
                }
            } else {
//                $stime = strtotime("-1 day");
//                empty($etime) && $etime = strtotime(date('Y-m-d'));
//                $where .= " AND pdc_add_time >= $stime AND pdc_add_time < $etime";
                $time = date('Y-m-d',time());
                $stime = strtotime($time);
                $where .= " AND pdc_add_time >= $stime";
            }
            $model = Model('predeposit');
            //$excel = $model->getCashList($where,'*',0,'id desc',$snum.','.$enum);
            $excel = $model->getPdCashList($where,30,'*','pdc_id desc',$snum.','.$enum);
            empty($excel) && showMessage('该时间段没有相应数据', '', '', 'error');
            foreach($excel as $k=>$v){
                switch ($v['state']){
                    case 0:
                        $v['state'] = '未审核';
                        break;
                    case 1:
                        $v['state'] = '已审核';
                        break;
                    case 2:
                        $v['state'] = '已关闭';
                        break;
                }
                switch ($v['pdc_payment_state']){
                    case 0:
                        $v['pdc_payment_state'] = '未支付';
                        break;
                    case 1:
                        $v['pdc_payment_state'] = '已支付';
                        break;
                }
                $excel[$k]['state'] = $v['state'];
                $excel[$k]['pdc_payment_state'] = $v['pdc_payment_state'];
                empty($v['pdc_add_time']) ? $addtime = '' : $addtime = date('Y-m-d H:i:s', $v['pdc_add_time']);
                empty($v['pdc_payment_time']) ? $pdc_payment_time = '' : $pdc_payment_time = date('Y-m-d H:i:s', $v['pdc_payment_time']);
                $excel[$k]['pdc_add_time'] = $addtime;
                $excel[$k]['pdc_payment_time'] = $pdc_payment_time;
            }
            $title = array('提现单号', '会员名称', '提现人', '支付宝账号', '提现金额', '审核状态', '提现状态', '提现时间', '审核时间', '备注信息');
            $field = array('pdc_sn', 'pdc_member_name', 'pdc_bank_user', 'pdc_bank_no', 'pdc_amount',  'state', 'pdc_payment_state','pdc_add_time','pdc_payment_time', 'remark');
            createExcel($title,$field,'提现表',$excel);

        }
    }
}